// JavaScript Document
(function ($) {
    $.extend({
        "login": function () {
            var $win = $(window), $loginDiv = $('#login'), $login = $('#login_button'), $errTip = $('#error_tips'), $capsLock = $('#error_tips'), showLogin = function () {
                $loginDiv.css({
                    top: ($win.height() - $loginDiv.height()) / 2 - 20 + 'px',
                    left: ($win.width() - $loginDiv.width()) / 2 + 'px'
                });
            }, $userTip = $('#uin_tips'), $passTip = $('#pwd_tips'), $code_tips = $("#code_tips"), $u = $('#u'), $p = $('#p'),$n = $('#n');
            $win.resize(function () { if ($loginDiv.is(':visible')) { showLogin(); } });
			showLogin();
             $(".W_input").bind({
				 
                'focus': function () {
				 	var $this = $(this);
					$this.addClass("W_input_focus");
					$this.parent(".inp").addClass("W_input_focus2");
					var $currTip = $(this).prev(".input_tips");
                    $currTip.css('color', '#ddd');
                },
                'blur': function () {
				 	var $this = $(this);
					$this.removeClass("W_input_focus");
					$this.parent(".inp").removeClass("W_input_focus2");
					var $currTip = $this.prev(".input_tips");
                    $currTip.css('color', '#c2c1c1');
                    if ($currTip === $passTip && $capsLock.is(':visible')) { $capsLock.hide(); }
                },
                'input': function (e) {
					var $this = $(this);
					var $delUserInput = $this.next(".uin_del");
					var $passhow = $this.next(".labelautybox");
					var $currTip = $this.prev(".input_tips");
                    if ($this.val()) {
						$currTip.hide();
						$delUserInput.show();
						$passhow.show();
                    } else {
                        $currTip.show();
                        $delUserInput.hide();
						$passhow.hide();
                    }
                }
            });
            
            function validCaptcha(e){
            	if($n.val()){
	            	$.ajax({    
	        		    url:contextPath+"/validCaptcha",   
	        		    data:{"captcha":$n.val()},    
	        		    type:"get",    
	        		    cache:false,    
	        		    dataType:"json",
	        		    async:true, //默认为true 异步
	        		    global:false,//是否触发全局 AJAX 事件。默认值: true
	        		    success:function(data) {
	        		    	if(typeof data == "string"){
	                    		if(data == "true"){
	                    			data = true;
	                    		}else{
	                    			data = false;
	                    		}
	                    	}
	        		    	if(data){
	        		    		$("#codelabel").removeClass("error").addClass("success");
	        		    	}else{
	        		    		$("#codelabel").removeClass("success").addClass("error");
	        		    	}
	        		     },    
	        		     error : function(XMLHttpRequest, textStatus, errorThrown) {
	        		    	 $errTip.html(XMLHttpRequest.responseText).show();
	        	  		 }   
	        		});
            	}
            }
            $n.keyup(function(e){
            	//排除回车
            	if(event.keyCode != '13'){
            		validCaptcha(e);
            	}
            });
            $(".jcaptcha-btn").click(function() {
                $(".jcaptcha-img").attr("src", contextPath+'/jcaptcha.jpg?'+new Date().getTime());
            });
            $p.keypress(function (e) {
                if (((e.keyCode >= 65 && e.keyCode <= 90) && !e.shiftKey) || ((e.keyCode >= 97 && e.keyCode <= 122) && e.shiftKey)) { 
				$errTip.text('大写锁定已打开！').show();
				} else {
					if($errTip.text() == '大写锁定已打开！'){
						$errTip.text('').hide();
					}
				}
            });
			var $delUserInput = $(".W_input").next(".uin_del");
            var $passhow = $(".W_input").next(".labelautybox");
            $delUserInput.click(function () {$(this).prev().val('').focus(); $(this).prev().prev(".input_tips").show(); $delUserInput.hide();});
            /*$passhow.children("input:checkbox").change(function () {
				var Pss = $(this).parent().parent().children("input")
				if($(this).checked==true){
					Pss.html("<input type='text' name='text' />");
				}else{
					Pss.attr('type','text');
				}
			});*/
			 $.fn.togglePassword = function( options ) {
				var s = $.extend( $.fn.togglePassword.defaults, options ),
				input = $( this );
		
				$( s.el ).bind( s.ev, function() {
					"password" == $( input ).attr( "type" ) ?
						$( input ).attr( "type", "text" ) :
						$( input ).attr( "type", "password" );
				});
			};
			$.fn.togglePassword.defaults = {
				ev: "click"
			};
            $login.click(function () {
            	$errTip.text('').hide();/*add by shuliangxing*/
            	if(!$u.val()){
            		$errTip.text('您还没有输入手机号码！').show();
            		return false;
            	}
            	else if (!/^1\d{10}$/.test($u.val())) {
                    $errTip.text('请输入正确的手机号码！').show();
                    return false;
                } else if (!$p.val()) {
                    $errTip.text('您还没有输入密码！').show();
                    return false;
                } else if (!$n.val() && $n.length > 0) {
                    $errTip.text('您还没有输入验证码！').show();
                    return false;
                } else {
                	if($n.length > 0){
	                	$.ajax({    
		        		    url:contextPath+"/validCaptcha",   
		        		    data:{"captcha":$n.val()},    
		        		    type:"get",    
		        		    cache:false,    
		        		    dataType:"json",
		        		    async:false, //默认为true 异步
		        		    global:false,//是否触发全局 AJAX 事件。默认值: true
		        		    success:function(data) {
		        		    	if(typeof data == "string"){
		                    		if(data == "true"){
		                    			data = true;
		                    		}else{
		                    			data = false;
		                    		}
		                    	}
		        		    	if(data){
		        		    		$("#codelabel").removeClass("error").addClass("success");
		        		    		$("#loginForm").submit();
		        		    	}else{
		        		    		$("#codelabel").removeClass("success").addClass("error");
		        		    		$errTip.text('验证码错误').show();
		        		    	}
		        		     },    
		        		     error : function(XMLHttpRequest, textStatus, errorThrown) {
		        		    	 $errTip.html(XMLHttpRequest.responseText).show();
		        	  		 }   
		        		});
                	}else{
                		$("#loginForm").submit();
                	}
                	
                }
            });
        }
    });
}(jQuery));